package com.jiamao.goplay.data;

import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;


import com.jiamao.goplay.data.source.http.HttpDataSourceImpl;
import com.jiamao.goplay.data.source.http.service.ApiService;
import com.jiamao.goplay.data.source.local.LocalDataSourceImpl;

import me.goldze.mvvmhabit.base.BaseModel;

/**
 * MVVM的Model层，统一模块的数据仓库，包含网络数据和本地数据（一个应用可以有多个Repositor）
 * Created by goldze on 2019/3/26.
 */
public class DataRepository extends BaseModel  {
    private volatile static DataRepository INSTANCE = null;
    private final HttpDataSourceImpl httpData;

    private final LocalDataSourceImpl mLocalDataSource;

    private DataRepository(@NonNull HttpDataSourceImpl httpDataSource,
                           @NonNull LocalDataSourceImpl localDataSource) {
        this.httpData = httpDataSource;
        this.mLocalDataSource = localDataSource;
    }

    public static DataRepository getInstance(HttpDataSourceImpl httpDataSource,
                                             LocalDataSourceImpl localDataSource) {
        if (INSTANCE == null) {
            synchronized (DataRepository.class) {
                if (INSTANCE == null) {
                    INSTANCE = new DataRepository(httpDataSource, localDataSource);
                }
            }
        }
        return INSTANCE;
    }

    @VisibleForTesting
    public static void destroyInstance() {
        INSTANCE = null;
    }


    public ApiService getApi(){
        return httpData.getApi();
    }

    public LocalDataSourceImpl getmLocalDataSource(){
        return mLocalDataSource;
    }


    public void saveUserName(String userName) {
        mLocalDataSource.saveUserName(userName);
    }

    public void savePassword(String password) {
        mLocalDataSource.savePassword(password);
    }

    public String getUserName() {
        return mLocalDataSource.getUserName();
    }

    public String getPassword() {
        return mLocalDataSource.getPassword();
    }


}
